package autoteam.ui.dialogs;

import java.awt.*;
import javax.swing.*;

import db.*;
import autoteam.tables.AutoTeamTableModel;

public class SearchTeamDialog extends AutoTeamDialog
{
private static final long serialVersionUID = 1L;
	
	private DataAccessObject dao;
	
	private JLabel team;
	private final JLabel sportLabel = new JLabel("Sport:");
	private final JLabel coachLabel = new JLabel("Coach:");
	private final JLabel playersLabel = new JLabel("Players:");
	private final JLabel nightLabel = new JLabel("Practice Night:");
	
	private JLabel sport;
	private JLabel coach;
	private JLabel[] players;
	private JLabel night;
	
	public SearchTeamDialog(JFrame dialogOwner, AutoTeamTableModel model, int row)
	{
		super (dialogOwner);
		
		String temp = (String) model.getValueAt(row, 0);
		team = new JLabel(temp);
		
		dao = new DataAccessObject();
		ColumnData col = new ColumnData("teamname", temp);
		DBRecord record = dao.getRecord("teams", col);
		
		if (record.getFieldValue("sport") != null)
			sport = new JLabel(record.getFieldValue("sport").toString());
		else
			sport = new JLabel("not found");
		
		if (record.getFieldValue("coach") != null)
		{
			int teamId = (Integer) record.getFieldValue("coach");
			ColumnData idCol = new ColumnData("id", teamId);
			DBRecord rec = dao.getRecord("coaches", idCol);
			String last = rec.getFieldValue("lastname").toString();
			String first = rec.getFieldValue("firstname").toString();
			coach = new JLabel(last + ", " + first);
		}
		else
			coach = new JLabel("not found");
		
		if (record.getFieldValue("practiceNight") == null)
			night = new JLabel("not found");
		else
		{
			int n = (Integer) record.getFieldValue("practiceNight");
			String str = "";
			if (n == 0) str = "Monday";
			else if (n == 1) str = "Tuesday";
			else if (n == 2) str = "Wednesday";
			else if (n == 3) str = "Thursday";
			else str = "Friday";
			
			night = new JLabel(str);
		}
		
		if (record.getFieldValue("players") == null)
			players = new JLabel[0];
		else
		{
			String s = record.getFieldValue("players").toString();
			String[] token = s.split(",");
			players = new JLabel[token.length];
			for (int i = 0; i < token.length; i++)
			{
				int p = Integer.parseInt(token[i]);
				ColumnData c = new ColumnData("id", p);
				DBRecord rec = dao.getRecord("players", c);
				String last = rec.getFieldValue("lastname").toString();
				String first = rec.getFieldValue("firstname").toString();
				if (last != null && first != null)
					players[i] = new JLabel(last + ", " + first);
				else
					players[i] = new JLabel("player not found");
			}
		}
		
		// set dialog preferences
		int r = 3 + players.length;
		int dialogWidth = 400;
		int dialogHeight = 170 + (30 * r);
		this.setDialogSize(new Dimension(dialogWidth, dialogHeight));
		Toolkit sizer = Toolkit.getDefaultToolkit(); 
		Dimension screenSize = sizer.getScreenSize();
		int screenCenterX = (screenSize.width - dialogWidth) / 2;
		int screenCenterY = (screenSize.height - dialogHeight) / 2;
		this.setLocation(screenCenterX, screenCenterY);
		
		// set layout preferences
		setLayout(null);
		int x = 50;		// x coordinate
		int y = 50;		// y coordinate
		int xGap = 100;	// gap between columns
		int yGap = 30;	// gap between rows
		
		int x1 = 100;	// column one width
		int x2 = 200;	// column two width
		int y1 = 20;	// row height
		
		int i = 0;
		team.setBounds(x, y, x1 + x2, y1);
		team.setHorizontalAlignment(JLabel.CENTER);
		i++;
		i++;
		sportLabel.setBounds(x, y + (yGap * i), x1, y1);
		sport.setBounds(x + xGap, y + (yGap * i++), x2, y1);
		coachLabel.setBounds(x, y + (yGap * i), x1, y1);
		coach.setBounds(x + xGap, y + (yGap * i++), x2, y1);
		nightLabel.setBounds(x, y + (yGap * i), x1, y1);
		night.setBounds(x + xGap, y + (yGap * i++), x2, y1);
		playersLabel.setBounds(x, y + (yGap * i), x1, y1);
		for (int j = 0; j < players.length; j++)
			players[j].setBounds(x + xGap, y + (yGap * i++), x2, y1);
		
		// add components
		add(team);
		add(sportLabel);
		add(sport);
		add(coachLabel);
		add(coach);
		add(playersLabel);
		for (int j = 0; j < players.length; j++)
		{
			add(players[j]);
		}
		add(nightLabel);
		add(night);
	}

	@Override
	protected void setButtonActions(AutoTeamDialog thisDialog) {
		// TODO Auto-generated method stub
		
	}
}
